﻿using System;
using System.Collections.Generic;
using Projecturo.Model;
using Projecturo.ProjecturoClient.Connection;
using Neits.PersistenceService.Providers;
using Projecturo.ProjecturoClient.Command;
using System.Windows.Input;

namespace Projecturo.ProjecturoClient.ViewModel.Project
{
    public class MilestoneViewModel : WorkspaceViewModel
    {
        private Milestone milestone;
        private DelegateCommand<object> openTicketCommand;

        public MilestoneViewModel()
        {
            milestone = new Milestone();
            milestone.FinishingDate = DateTime.Now;
        }

        public override string DisplayName
        {
            get
            {
                if (milestone != null)
                {
                    return String.IsNullOrEmpty(milestone.Name) ? "New Milestone" : milestone.Name;
                }
                return "New Milestone";
            }
            protected set
            {
                base.DisplayName = value;
            }
        }

        public ICommand OpenTicketCommand
        {
            get
            {
                if (openTicketCommand == null)
                    openTicketCommand = new DelegateCommand<object>(OpenTicket);
                return openTicketCommand;
            }
        }

        private void OpenTicket(object ticket)
        {
            Ticket ticketToOpen = ticket as Ticket;
            if (ticketToOpen != null)
            {
                ViewModelBase viewModel = ViewModelManager.Instance.CreateViewModel(typeof (Ticket), this);
                viewModel.SetModel(ticketToOpen);
                AddToWorkspace(viewModel as WorkspaceViewModel, ViewModelReference, viewModel as WorkspaceViewModel);
            }
        }

        public Int64 Id
        {
            get
            {
                return milestone.Id;
            }
        }

        public string Name
        {
            get { return milestone.Name; }
            set
            {
                if (milestone.Name == value)
                    return;
                milestone.Name = value;
                OnPropertyChanged("Name");
            }
        }

        public string Description
        {
            get { return milestone.Description; }
            set
            {
                if (milestone.Description == value)
                    return;
                milestone.Description = value;
                OnPropertyChanged("Description");
            }
        }

        public DateTime FinishingDate
        {
            get { return milestone.FinishingDate; }
            set
            {
                if (milestone.FinishingDate == value)
                    return;
                milestone.FinishingDate = value;
                OnPropertyChanged("FinishingDate");
            }
        }

        public Model.Project Project
        {
            get { return milestone.Project; }
            set
            {
                if (milestone.Project == value)
                    return;
                milestone.Project = value;
                OnPropertyChanged("Project");
            }
        }

        public IList<Ticket> Tickets
        {
            get
            {
                var filter = new HqlFilter("from Ticket t where t.Milestone.Id = " + Id);
                IList<Ticket> tickets = Connector.Connectivity.Storage.GetList<Ticket>(filter);
                if (tickets == null)
                    return new List<Ticket>();
                return tickets;
            }
        }

        protected override void Save()
        {
            Connector.Connectivity.Storage.Save(milestone);
            ViewModelBase parentViewModel = ViewModelManager.Instance.GetViewModel(ViewModelReference.ParentViewModelId);
            parentViewModel.Update();
        }

        public override void SetModel(long id)
        {
            milestone = Connector.Connectivity.Storage.Get<Milestone>(id);
        }

        public override void SetParent(object parent)
        {
            Project = parent as Model.Project;
        }
    }
}
